package cn.yusei.teamTribe.controller.sign;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.service.sign.SignService;
import cn.yusei.teamTribe.service.user.TokenService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class SignController {
	   @Autowired
	    SignService signService;
	   @Autowired
	   TokenService tokenService;

	   @RequestMapping(value = "/sign/getSentence")
	   public void getSentence(PrintWriter out){
		   Random random = new Random();
		   List<String> sentences = null;
		   try {
			sentences = signService.getSentence();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
			return;
		}
		   Map<String, Object> param = new HashMap<>();
		   param.put("sentence", sentences.get(random.nextInt(10)));
		   ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);	   	   
		   
	   }
	   
	    @RequestMapping(value = "/sign/signToday",method = RequestMethod.POST)
	    public void signToday(@RequestAttribute("id") int id,PrintWriter out){
	        int code = -1;
	        try {
	            code = signService.SignToday(id);
	        } catch (Exception e) {
	            e.printStackTrace();
	            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
	        }
	        ResponseUtil.responseCode(out,code);
	    }

	    /**
	     * 获取连续签到天数
	     * @param id
	     * @param session
	     * @param response
	     */
	    @RequestMapping(value = "/sign/getSignDay",method = RequestMethod.POST)
	    public void getSignDay(@RequestAttribute("id") int id, PrintWriter out){
	        Integer day = null;
	        try {
	            day = signService.getSignDay(id);
	        } catch (Exception e) {
	            e.printStackTrace();
	            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
	        }
	        Map<String,Object> param = new HashMap<>();
	        param.put("day",day);
	        if(day == null){
	            param.put("day",0);
	        }
	        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

	    }

	    @RequestMapping(value = "/sign/sign",method = RequestMethod.POST)
	    public void sign(@RequestAttribute("id") int id,PrintWriter out){
            int code = -1;
            try {
                code = signService.sign(id);
            }catch (Exception e){
                e.printStackTrace();
                ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            }
            ResponseUtil.responseCode(out,code);

	    }
	

}
